import Vue from 'vue';
import Vuex from 'vuex';
import user from './model/user';
import api from '../utils/apijs';
import MedicalRecords from './model/MedicalRecords/MedicalRecords';
import bingli from './model/MedicalRecords/bingli';
import guojijibing from './model/MedicalRecords/guojijibing';
import { getStorage, setStorage, removeStorage } from '../assets/js/storage';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    dianji: false,
    list: [],
    caozuoid: '',
    users: getStorage('user'), //{ id: 1, name: '李四' },
  },
  getters: {},
  mutations: {
    setcaozuoid(state, data) {
      state.caozuoid = data;
    },
    setlist(state, data) {
      state.list = data;
      state.dainji = true;
    },
    saveMyUser(state, data) {
      state.users = data;
      setStorage('user', data[1]);
      setStorage('token', data[0]);
    },
  },
  actions: {
    tongyong(context, obj) {
      return api[obj.name](obj.data).then(data => {
        return data;
      });
    },
    saveUser(context, obj) {
      return api[obj.name](obj.data).then(data => {
        context.commit('saveMyUser', data.data.data);
        return data;
      });
    },
    // saveUser(context, obj) {
    //   return api[obj.name](obj.data).then(data => {
    //     context.commit('saveMyUser', data.data.data);
    //     return data;
    //   });
    // },
    chaxunsuoyou(context, obj) {
      return api[obj.name](obj.data).then(data => {
        console.log(data);
        return data;
      });
    },
    xiugaibaoxiaodan(context, obj) {
      return api[obj.name](obj.data).then(data => {
        return data.data.code;
      });
    },
    shanchubaoxiaodan(context, obj) {
      return api[obj.name](obj.data).then(data => {
        return data;
      });
    },
  },
  modules: {
    user,
    MedicalRecords,
    bingli,
    guojijibing,
  },
});
